package com.hansen.library.ui.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.hansen.library.R;
import com.hansen.library.utils.ColorUtils;

/**
 * @author han
 * @date 2019/9/6
 */
public class LineView extends View {
    private int mLineColor;
    private int mLineHeight;
    private int mLinePadding;

    private boolean mHasLineEndPadding;
    private boolean mHasLineStartPadding;

    private int mOrientation;

    private Paint mPaint;
    private Path mPath;

    public LineView(Context context) {
        this(context, null);
    }

    public LineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LineViewStyleable);

        mLineHeight = typedArray.getDimensionPixelOffset(R.styleable.LineViewStyleable_line_height, 2);
        mLineColor = typedArray.getColor(R.styleable.LineViewStyleable_line_color, ColorUtils.getColorById(context, R.color.color_line));
        mLinePadding = typedArray.getDimensionPixelOffset(R.styleable.LineViewStyleable_line_padding, 0);

        mHasLineStartPadding = typedArray.getBoolean(R.styleable.LineViewStyleable_line_has_start_padding, false);
        mHasLineEndPadding = typedArray.getBoolean(R.styleable.LineViewStyleable_line_has_end_padding, false);

        mOrientation = typedArray.getInt(R.styleable.LineViewStyleable_android_orientation, LinearLayout.HORIZONTAL);

        typedArray.recycle();

        mPaint = new Paint();

        mPath = new Path();

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mLineColor);
        mPaint.setStrokeWidth(mLineHeight);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(mOrientation == LinearLayout.VERTICAL) {
            int heightSize = measureHeight(heightMeasureSpec);

            setMeasuredDimension(mLineHeight, heightSize);
        } else {
            int widthSize = measureWidth(widthMeasureSpec);

            setMeasuredDimension(widthSize, mLineHeight);
        }
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //设置一个默认值，就是这个View的默认宽度为1，这个看我们自定义View的要求
        int result = 2;

        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值
            result = specSize;
        }

        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 2;

        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }

        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPath.reset();

        if(mOrientation == LinearLayout.VERTICAL) {
            mPath.moveTo((float) (0 + mLineHeight / 2.0), mHasLineStartPadding ? (0 + mLinePadding) : 0);
            mPath.lineTo((float) (0 + mLineHeight / 2.0), mHasLineEndPadding ? (getHeight() - mLinePadding) : getHeight());
        } else {
            mPath.moveTo(mHasLineStartPadding ? (0 + mLinePadding) : 0, (float) (0 + mLineHeight / 2.0));
            mPath.lineTo(mHasLineEndPadding ? (getWidth() - mLinePadding) : getWidth(), (float) (0 + mLineHeight / 2.0));
        }

        canvas.drawPath(mPath, mPaint);
    }
}
